#include "document.h"

#include <iostream>
#include <iomanip>
#include <typeinfo>
#include <sstream>

Document::Document()
{
}

Document::Document(const int& p_type):type(p_type)
{
}

Document::~Document()
{
}

int Document::get_type() const
{
	return(type);
}

int Document::get_issue_no() const
{
	return(issue_no);
}

Date Document::get_date() const
{
	return(date);
}

std::string Document::get_subject() const
{
	return(subject);
}

std::string Document::get_partner() const
{
	return(partner);
}

int Document::get_total() const
{
	return(total);
}

int Document::get_ref_no() const
{
	return(ref_no);
}

std::ostream& operator<<(std::ostream &p_out_stream, const Document &p_document)
{
	const bool p_to_std_cout=(typeid(p_out_stream)==typeid(std::cout));
	if(p_to_std_cout)
	{
		std::cout<<"------------------------"<<std::endl;
		std::cout<<"Irat tipusa: ";
		std::cout<<Document::document_type_name(p_document.type)<<std::endl;;
	}
	else
	{
		p_out_stream<<p_document.type<<std::endl;
	}
	if(p_to_std_cout)
	{
		std::cout<<"Ugyszam:   : ";
	}
	p_out_stream<<p_document.issue_no<<std::endl;
	if(p_to_std_cout)
	{
		std::cout<<"Kelt       : ";
	}
	Date p_d;
	p_out_stream<<p_document.date<<std::endl;
	if(p_to_std_cout)
	{
		std::cout<<"Targy      : ";
	}
	p_out_stream<<p_document.subject<<std::endl;
	if(p_to_std_cout)
	{
		std::cout<<"Partner    : ";
	}
	p_out_stream<<p_document.partner;
	if(1==p_document.type)
	{
		if(p_to_std_cout)
		{
			std::cout<<std::endl<<"Osszertek  : ";
		}
		else
		{
			p_out_stream<<std::endl;
		}
		p_out_stream<<p_document.get_total();
	}
	if(p_to_std_cout)
	{
		if(0!=p_document.ref_no)
		{
			std::cout<<std::endl<<"Iktatoszam : ";
			p_out_stream<<p_document.ref_no;
		}
	}
	else
	{
		p_out_stream<<std::endl<<p_document.ref_no;
	}
	return(p_out_stream);
}

std::istream& operator>>(std::istream &p_in_stream, Document &p_document)
{
	const bool p_from_std_cin=(typeid(p_in_stream)==typeid(std::cin));
	std::string p_string;
	std::istringstream* p_s;
	if(p_from_std_cin)
	{
		//std::cout<<"Irat tipusa? ";
	}
	else
	{
		getline(p_in_stream,p_string);
		p_s=new std::istringstream(p_string);
		(*p_s)>>std::noskipws>>p_document.type;
		delete p_s;
	}
	if(p_from_std_cin)
	{
		std::cout<<"Ugyszam    ? ";
	}
	getline(p_in_stream,p_string);
	p_s=new std::istringstream(p_string);
	(*p_s)>>std::noskipws>>p_document.issue_no;
	delete p_s;
	if(p_from_std_cin)
	{
		std::cout<<"Kelt       ? ";
	}
	getline(p_in_stream,p_string);
	p_s=new std::istringstream(p_string);
	(*p_s)>>std::noskipws>>p_document.date;
	delete p_s;
	if(p_from_std_cin)
	{
		std::cout<<"Targy      ? ";
	}
	getline(p_in_stream,p_string);
	p_document.subject=p_string;
	if(p_from_std_cin)
	{
		//std::cout<<p_string<<std::endl;
		std::cout<<"Partner    ? ";
	}
	getline(p_in_stream,p_string);
	p_document.partner=p_string;
	if(1==p_document.type)
	{
		if(p_from_std_cin)
		{
			std::cout<<"Osszertek  ? ";
		}
	getline(p_in_stream,p_string);
	p_s=new std::istringstream(p_string);
	(*p_s)>>std::noskipws>>p_document.total;
	}
	if(p_from_std_cin)
	{
		std::cout<<"Iktatoszam ? ";
	}
	getline(p_in_stream,p_string);
	p_s=new std::istringstream(p_string);
	(*p_s)>>std::noskipws>>p_document.ref_no;
	delete p_s;
	if(p_from_std_cin)
	{
		//std::cout<<p_string<<std::endl;;
		std::cout<<std::endl;
		std::cout<<"Rendben, a kovetkezo adatokat rogzitettuk:"<<std::endl;
		std::cout<<p_document<<std::endl<<"------------------------";
	}
	return(p_in_stream);
}

std::string Document::document_type_name(const int p_type)
{
	switch(p_type)
	{
		case ORDER:
		{
			return("Hatarozat");
			break;
		}
		case BILL:
		{
			return("Szamla");
			break;
		}
		case CLAIM:
		{
			return("Panaszos level");
			break;
		}
		case TENDER:
		{
			return("Ajanlatkero level");
			break;
		}
		default:
		{
			return("Ismeretlen tipus");
			break;
		}
	}
}